<?php

/*
 * [UCenter Home] (C) 2007-2008 Comsenz Inc. $Id: function_cp.php 13245
 * 2009-08-25 02:01:40Z liguode $
 */

if (! defined ( 'IN_UCHOME' )) {
	exit ( 'Access Denied' );
}

// 保存图片
function file_save($FILE, $title = "", $uid = 0, $username = '', $allowpictype = array()) {
	global $_SGLOBAL, $_SC;
	try {
		
		// 检查
		$FILE ['size'] = intval ( $FILE ['size'] );
		if (empty ( $FILE ['size'] ) || empty ( $FILE ['tmp_name'] ) || ! empty ( $FILE ['error'] )) {
			return cplang ( 'lack_of_access_to_upload_file_size' );
		}
		
		// 判断后缀
		$fileext = fileext ( $FILE ['name'] );
		
		if ($allowpictype) {
			if (! in_array ( $fileext, $allowpictype )) {
				return cplang ( '上传的文件格式不正确' );
			}
		}
		// 获取目录
		if (! $filepath = getfilepath ( $fileext, true )) {
			return cplang ( 'unable_to_create_upload_directory_server' );
		}
		
		// 本地上传
		$new_name = $_SC ['attachdir'] . './' . $filepath;
		$tmp_name = $FILE ['tmp_name'];
		if (@ copy ( $tmp_name, $new_name )) {
			@ unlink ( $tmp_name );
		} elseif ((function_exists ( 'move_uploaded_file' ) && @ move_uploaded_file ( $tmp_name, $new_name ))) {
		} elseif (@ rename ( $tmp_name, $new_name )) {
		} else {
			return cplang ( 'mobile_picture_temporary_failure' );
		}
		
		// 入库
		$title = getstr ( $title, 200, 1, 1, 1 );
		
		// 入库
		$setarr = array (
				'uid' => $uid ? $uid : $_SGLOBAL ['supe_uid'],
				'username' => $username ? $username : $_SGLOBAL ['supe_username'],
				'dateline' => $_SGLOBAL ['timestamp'],
				'filename' => addslashes ( $FILE ['name'] ),
				'postip' => getonlineip (),
				'title' => $title,
				'type' => addslashes ( $FILE ['type'] ),
				'size' => $FILE ['size'],
				'filepath' => $filepath,
				'ext' => $fileext 
		);
		$setarr ['fileid'] = inserttable ( 'z_file', $setarr, 1 );
		return $setarr;
	} catch ( Exception $e ) {
		return "";
	}
}

// 保存图片
function pic_save($FILE, $album = "picture",$uid = 0, $username = '', $title = "", $topic = '',$orgUrl='',$referUrl='') {
	global $_SGLOBAL, $_SC;
	try {
		// 允许上传类型
		$allowpictype = array (
				'jpg',
				'jpeg',
				'gif',
				'png' 
		);
		
		// 检查
		$FILE ['size'] = intval ( $FILE ['size'] );
		if (empty ( $FILE ['size'] ) || empty ( $FILE ['tmp_name'] ) || ! empty ( $FILE ['error'] )) {
			return cplang ( 'lack_of_access_to_upload_file_size' );
		}
		
		// 判断后缀
		$fileext = fileext ( $FILE ['name'] );
		if (! in_array ( $fileext, $allowpictype )) {
			return cplang ( 'only_allows_upload_file_types' );
		}
		
		// 获取目录
		if (! $filepath = getfilepath ( $fileext, true )) {
			return cplang ( 'unable_to_create_upload_directory_server' );
		}
		
		// 本地上传
		$new_name = $_SC ['attachdir'] . './' . $filepath;
		$tmp_name = $FILE ['tmp_name'];
		if (@copy ( $tmp_name, $new_name )) {
			@unlink ( $tmp_name );
		} elseif ((function_exists ( 'move_uploaded_file' ) && @move_uploaded_file ( $tmp_name, $new_name ))) {
		} elseif (@rename ( $tmp_name, $new_name )) {
		} else {
			return cplang ( 'mobile_picture_temporary_failure' );
		}
		
		// 检查是否图片
		if (function_exists ( 'getimagesize' )) {
			$tmp_imagesize = @getimagesize ( $new_name );
			list ( $tmp_width, $tmp_height, $tmp_type ) = ( array ) $tmp_imagesize;
			$tmp_size = $tmp_width * $tmp_height;
			//$tmp_size>16777216  //$tmp_size > 3240000 ||
			if ( $tmp_size < 4 || empty ( $tmp_type ) || strpos ( $tmp_imagesize ['mime'], 'flash' ) > 0) {
				@unlink ( $new_name );
				return cplang ( 'only_allows_upload_file_types' );
			}
		}
		
		// 缩略图
		include_once (S_ROOT . './api/function_image.php');
		$set_arr = array_keys($_SGLOBAL['set_arr']);
// 		print_r($set_arr);exit;
		$thumbArr = array (); // 缩略图
// 		foreach ( $set_arr as $v ) {
// 			if (in_array ( $v, array (
// 					"square",
// 					"avatart",
// 					"thumb" 
// 			) )) {
// 				$thumbpath = makethumbSquare ( $new_name, $v );
// 			} else {
// 				$thumbpath = makethumb ( $new_name, $v );
// 			}
// 			$thumbArr [$v] = empty ( $thumbpath ) ? 0 : 1;
// 		}
		
		// 是否压缩
		// 获取上传后图片大小
		if (@$newfilesize = filesize ( $new_name )) {
			$FILE ['size'] = $newfilesize;
		}
		
		// 水印
		if ($_SGLOBAL ['allowwatermark']) {
			makewatermark ( $new_name );
		}
		
		// 入库
		$setarr = array (
				'album' => $album,
				'uid' => $uid ? $uid : $_SGLOBAL ['supe_uid'],
				'username' => $username ? $username : $_SGLOBAL ['supe_username'],
				'dateline' => $_SGLOBAL ['timestamp'],
				'filename' => addslashes ( $FILE ['name'] ),
				'postip' => getonlineip (),
				'title' => $title,
				'type' => addslashes ( $FILE ['type'] ),
				'size' => $FILE ['size'],
				'filepath' => $filepath,
				'thumb' => serialize ( $thumbArr ),
				'remote' => 0 ,
				'topic' => $topic ,
				'org_url' => $orgUrl ,
				'referer' => $referUrl 
		);
		$setarr ['picid'] = inserttable ( 'pic', $setarr, 1 );
		$setarr ['thumbArr'] = $thumbArr;
		return $setarr;
	} catch ( Exception $e ) {
		return "内部错误";
	}
}

// 数据流保存，所有数据均为存放相册的所以写入的数据一定只能是图片
function stream_save($strdata, $album = "picture",  $uid = 0, $username = '', $title = '', $topic = '',$orgUrl='',$referUrl='',$fileext = 'jpg') {
	global $_SGLOBAL, $_SC;
	try {
		$setarr = array ();
		$filepath = getfilepath ( $fileext, true );
		$newfilename = $_SC ['attachdir'] . './' . $filepath;
		
		
		if ($handle = fopen ( $newfilename, 'wb' )) {
			if (fwrite ( $handle, $strdata ) !== FALSE) {
				fclose ( $handle );
				$size = filesize ( $newfilename );
				
				// 检查是否图片
				if (function_exists ( 'getimagesize' )) {
					$tmp_imagesize = @getimagesize ( $newfilename );
					list ( $tmp_width, $tmp_height, $tmp_type ) = ( array ) $tmp_imagesize;
					$tmp_size = $tmp_width * $tmp_height;
// 					echo $tmp_size;exit;
					if ($tmp_size > 3240000 || $tmp_size < 4 || empty ( $tmp_type ) || strpos ( $tmp_imagesize ['mime'], 'flash' ) > 0) {
						@unlink ( $newfilename );
						return "图片异常，可能图片太大或太小，或者图片格式不正确！";
					}
				}
				// 缩略图
				include_once (S_ROOT . './api/function_image.php');
				$set_arr = array_keys($_SGLOBAL['set_arr']);
				$thumbArr = array (); // 缩略图
// 				foreach ( $set_arr as $v ) {
// 					if (in_array ( $v, array (
// 							"square",
// 							"avatart",
// 							"thumb" 
// 					) )) {
// 						$thumbpath = makethumbSquare ( $newfilename, $v );
// 					} else {
// 						$thumbpath = makethumb ( $newfilename, $v );
// 					}
// 					$thumbArr [$v] = empty ( $thumbpath ) ? 0 : 1;
// 				}
				
				// 大头帖不添加水印
				if ($_SGLOBAL ['allowwatermark']) {
					makewatermark ( $newfilename );
				}
				
				// 入库
				$filename = addslashes (  substr ( strrchr ( $filepath, '/' ), 1 ));
				$title = getstr ( $title, 200, 1, 1, 1 );
				
				$setarr = array (
						'album' => $album,
						'uid' => $uid ? $uid : $_SGLOBAL ['supe_uid'],
						'username' => $username ? $username : $_SGLOBAL ['supe_username'],
						'dateline' => $_SGLOBAL ['timestamp'],
						'filename' => $filename,
						'postip' => getonlineip (),
						'title' => $title,
						'type' => $fileext,
						'size' => $size,
						'filepath' => $filepath,
						'thumb' => serialize ( $thumbArr ),
						'remote' => 1 ,
						'topic' => $topic ,
						'org_url' => $orgUrl ,
						'referer' => $referUrl 
				);
				$setarr ['picid'] = inserttable ( 'pic', $setarr, 1 );
				$setarr ['thumbArr'] = $thumbArr;
				return $setarr;
			} else {
				fclose ( $handle );
			}
		}
	} catch ( Exception $e ) {
		return "内部错误";
	}
}
// 获取上传路径
function getfilepath($fileext, $mkdir = false) {
	global $_SGLOBAL, $_SC;
	
	$filepath = "{$_SGLOBAL['supe_uid']}_{$_SGLOBAL['timestamp']}" . random ( 4 ) . ".$fileext";
	$name1 = gmdate ( 'Ym' );
	$name2 = gmdate ( 'j' );
	
	if ($mkdir) {
		$newfilename = $_SC ['attachdir'] . './' . $name1;
		if (! is_dir ( $newfilename )) {
			if (! @mkdir ( $newfilename )) {
				//runlog ( 'error', "DIR: $newfilename can not make" );
				return $filepath;
			}
		}
		$newfilename .= '/' . $name2;
		if (! is_dir ( $newfilename )) {
			if (! @mkdir ( $newfilename )) {
				//runlog ( 'error', "DIR: $newfilename can not make" );
				return $name1 . '/' . $filepath;
			}
		}
	}
	return $name1 . '/' . $name2 . '/' . $filepath;
}

// 获取上传路径,360上传
function getfilepath1($filename, $fileext, $dirname, $mkdir = false) {
	global $_SGLOBAL, $_SC;
	
	$filepath = $filename . ".$fileext";
	$names = explode ( '/', $dirname );
	
	if ($mkdir) {
		$newfilename = $_SC ['attachdir'] . '.';
		$path = "";
		foreach ( $names as $v ) {
			$path .= $v . "/";
			$newfilename .= '/' . $v;
			if (! is_dir ( $newfilename )) {
				if (! @ mkdir ( $newfilename )) {
					//runlog ( 'error', "DIR: $newfilename can not make" );
				}
			}
		}
	}
	return $path . $filepath;
}

// 获取上传路径
function getfilepath2($fileext, $mkdir = false) {
	global $_SGLOBAL, $_SC;
	$filepath = "{$_SGLOBAL['supe_uid']}_{$_SGLOBAL['timestamp']}" . random ( 4 ) . ".$fileext";
	$name1 = gmdate ( 'Ym' );
	$name2 = gmdate ( 'j' );
	
	if ($mkdir) {
		$newfilename = $_SC ['swfdir'] . './' . $name1;
		if (! is_dir ( $newfilename )) {
			if (! @mkdir ( $newfilename )) {
				//runlog ( 'error', "DIR: $newfilename can not make" );
				return $filepath;
			}
		}
		$newfilename .= '/' . $name2;
		if (! is_dir ( $newfilename )) {
			if (! @mkdir ( $newfilename )) {
				//runlog ( 'error', "DIR: $newfilename can not make" );
				return $name1 . '/' . $filepath;
			}
		}
	}
	return $name1 . '/' . $name2 . '/' . $filepath;
}

// 获取上传路径
function getfilepath3($dirname, $dateline) {
	$name1 = gmdate ( 'Ym', $dateline );
	$name2 = gmdate ( 'j', $dateline );
	$name3 = $dirname;
	return $name1 . "/" . $name2 . "/" . $name3 . "";
}
?>